﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DesktableManager : MonoBehaviour
{

    [Header("此脚本为程序管理脚本")]
    [Header("大部分管理函数都在此脚本上")]
    [Header("其他脚本调用")]

    public GameObject Lead;
    public GameObject Cam;
    public GameObject Black;
    public GameObject DayPlane;

    public AudioSource A_Manager;

    public GameObject[] Icons;

    public Animation[] Texts;

    public Animation UP;
    public Animation DOWN;
    public Animation CamZoom;
    public Animation DayFade;

    public AnimationClip Zoomin;
    public AnimationClip Zoomout;

    public AnimationClip FadeIn;
    public AnimationClip FadeOut;

    public AudioClip Back_1;
    public AudioClip Back_2;

    public GameObject juQing;

    public GameObject FenXiInputPlane;

    bool move = false;
    bool ChangeVol = false;

    void Start()
    {

        Black.SetActive(false);

        Day();

        Clear();
    }

    public void ShowYinYan()
    {
        Black.SetActive(true);

        StartCoroutine(show());
    }

    IEnumerator show()
    {
        int i = 0;
        while (i < Texts.Length)
        {
            Texts[i].Play();
            yield return new WaitForSeconds(4);
            i++;
        }
        Black.SetActive(false);

        Day();
    }

    public void LeadJuQing(string text)
    {

    }

    public void BlackShowText(string text)
    {
        Black.SetActive(true);

        Black.GetComponent<Animation>().clip = FadeIn;
        Black.GetComponent<Animation>().Play();

        Black.GetComponentInChildren<Text>().text = text;

        Invoke("DoNotShow", 3f);
    }

    void DoNotShow()
    {
        FenXiInputPlane.GetComponent<FenxiInput>().Name = GameObject.Find("GameManager").GetComponent<DetailFindText>().Name.text.ToString();
        Instantiate(FenXiInputPlane, GameObject.Find("Canvas").transform);

        Black.GetComponent<Animation>().clip = FadeOut;
        Black.GetComponent<Animation>().Play();
    }


    public void Day()
    {
        DayPlane.SetActive(true);

        DayFade.Play();

        Invoke("DisAppear", 3.5f);
    }

    public void DisAppear()
    {
        DayPlane.SetActive(false);
    }

    public void Clear()
    {
        for (int i = 0; i < Icons.Length; i++)
        {
            Icons[i].GetComponent<APPManager>().APP.SetActive(false);
            Icons[i].GetComponent<APPManager>().Back.color = new Color(1, 1, 1, 0.01f);
        }
    }

    public void JuQing(int time)
    {
        move = true;

        StartCoroutine(ZoomIn());

        Invoke("End", time);
    }

    public void End()
    {
        move = false;

        StartCoroutine(ZoomOut());
    }

    IEnumerator ZoomIn()
    {
        juQing.SetActive(true);

        UP.Play();
        DOWN.Play();

        CamZoom.clip = Zoomin;
        CamZoom.Play();

        ChangeVol = true;

        yield return new WaitForSeconds(1f);
    }

    IEnumerator ZoomOut()
    {
        juQing.SetActive(false);

        CamZoom.clip = Zoomout;
        CamZoom.Play();


        ChangeVol = true;

        yield return new WaitForSeconds(1f);

    }

    void Update()
    {
        if (move)
        {
            for (int i = 0; i < gameObject.GetComponent<NPCManager>().NPC.Length; i++)
            {
                gameObject.GetComponent<NPCManager>().NPC[i].talkPlane.GetComponent<ScrollRect>().verticalScrollbar.value = 0;
            }
        }
        if (ChangeVol)
        {
            if (A_Manager.volume > 0f)
            {
                A_Manager.volume -= Time.deltaTime;
            }
            else
            {
                Change();
            }

        }
        else
        {
            if (A_Manager.volume <= 0.5f)
            {
                A_Manager.volume += Time.deltaTime;
            }
        }
    }

    void Change()
    {
        ChangeVol = false;

        if (A_Manager.clip == Back_1)
        {
            A_Manager.clip = Back_2;
        }
        else
        {
            A_Manager.clip = Back_1;
        }

        A_Manager.Play();
    }
}
